package com.csdj.t309springboot.controller;


import com.csdj.t309springboot.common.StatusContant;
import com.csdj.t309springboot.common.WebResult;
import com.csdj.t309springboot.common.WebResultMsgCode;
import com.csdj.t309springboot.entity.User;
import com.csdj.t309springboot.service.UserService;
import com.csdj.t309springboot.utils.EncryptUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 谭湛宇
 * @since 2021-12-31
 */
@RestController
@RequestMapping("/user")
public class UserController extends BaseController{

    private Logger log = LoggerFactory.getLogger("userController");
    @Resource
    private UserService userService;

    @GetMapping("/add")
    public int add(@RequestParam(value = "username",required = false) String username,
                   @RequestParam(value = "password",required = false) String password){
        log.info("username="+username+",password="+password);
        User user = new User();
        user.setUsername(username);
        user.setPassword(EncryptUtil.getMd5(password));
        int insert = userService.insert(user);
        log.info("insert="+insert);
        return insert;
    }

    @GetMapping("/delete")
    public WebResult detele(@RequestParam(value = "id",required = true) int id){
        log.info("id="+id);

        int delete = userService.delete(id);
        if(delete>0){
            return new WebResult(WebResultMsgCode.SUCCESS).putData("id",delete);
        }

        return null;
    }

    @ResponseBody
    @GetMapping("/list")
    public WebResult list(){
        List<User> list = userService.getAllUsers();
        if(null != list){
            return new WebResult(WebResultMsgCode.SUCCESS).putData("user",list);
        }
        return null;
    }

    /**
     * 模糊查询
     * @param column 列名
     * @return
     */
    @ResponseBody
    @GetMapping("/fuzzySearch")
    public WebResult fuzzySearch(@RequestParam(value = "column",required = false) String column){
        List<User> list = userService.getAllUsersByMoHuChaXun(column);
        if(null != list){
            return new WebResult(WebResultMsgCode.SUCCESS).putData("user",list);
        }
        return null;
    }



    @PostMapping("/login")
    public WebResult login(@RequestParam String username,
                           @RequestParam String password){
        User user = userService.login(username, password);
        if(user != null && StatusContant.USER_STATUS_LOCKED == user.getStatus()){
            return new WebResult(WebResultMsgCode.USER_LOCKED);
        }
        if(user == null){
            return new WebResult(WebResultMsgCode.USER_PWD_ERROR);
        }
        return new WebResult(WebResultMsgCode.SUCCESS).putData("user",user);
    }

    @GetMapping("/info")
    public WebResult info(HttpServletRequest request){
        int uid = getUid(request);
        log.info("uid="+uid);
        User user = userService.getUserInfoById(uid);
        return new WebResult(WebResultMsgCode.SUCCESS).putData("user",user);
    }



    @PostMapping("/update")
    public WebResult update(@RequestBody User user){
        log.info("user="+user);
        boolean update = userService.update(user);
        if(update){
            return new WebResult(WebResultMsgCode.SUCCESS);
        }
        return new WebResult(WebResultMsgCode.FAIL);
    }

}
